package com.digitalchina.lxnsy.viewholder;

import androidx.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.entity.ActivityListVO;
import com.digitalchina.lxnsy.interfaces.ICallBackActivity;
import com.digitalchina.lxnsy.toolkit.FrescoUtil;
import com.facebook.drawee.view.SimpleDraweeView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.viewholder
 * 创建人：郑皓
 * 创建时间：2018/7/17 11:10
 * 修改备注：
 * ================================================
 */

public class ActivityReleaseHolder extends BaseViewHolder<ActivityListVO> {

    private SimpleDraweeView ivPicture;
    private TextView tvState;
    private TextView tvPrice;
    private TextView tvTitle;
    private TextView tvAddress;
    private TextView tvTime;

    private TextView tvManage;
    private TextView tvAmend;
    private TextView tvCancel;

    private boolean isRelease;
    private ICallBackActivity iCallBackActivity;
    private String content = "";


    public ActivityReleaseHolder(ViewGroup parent, boolean isRelease, ICallBackActivity iCallBackActivity) {
        super(parent, R.layout.item_activity_list_release);
        ivPicture = $(R.id.iv_picture);
        tvState = $(R.id.tv_state);
        tvPrice = $(R.id.tv_price);
        tvTitle = $(R.id.tv_title);
        tvAddress = $(R.id.tv_address);
        tvTime = $(R.id.tv_time);

        tvManage = $(R.id.tv_manage);
        tvAmend = $(R.id.tv_amend);
        tvCancel = $(R.id.tv_cancel);

        this.isRelease = isRelease;
        this.iCallBackActivity = iCallBackActivity;

    }

    @Override
    public void setData(ActivityListVO data) {
        super.setData(data);

        FrescoUtil.showImageSmall(data.getImgPath(), ivPicture);
        tvState.setText(data.getActivityStateName() + "");
        tvTitle.setText(data.getActivityName() + "");
        tvTime.setText(data.getStartTime() + "");

        switch (data.getActivityStateName() + ""){
            case "即将开始":
                tvState.setBackgroundResource(R.color.activity_dengdai);
                break;
            case "正在进行":
                tvState.setBackgroundResource(R.color.activity_jinxing);
                break;
            case "已结束":
                tvState.setBackgroundResource(R.color.activity_jieshu);
                break;
        }

        if (isRelease){
            if ("已取消".equals(data.getActivityState()) || "已结束".equals(data.getActivityState())) {
                tvAmend.setVisibility(View.GONE);
                tvCancel.setVisibility(View.GONE);
            } else {
                tvAmend.setVisibility(View.VISIBLE);
                tvCancel.setVisibility(View.VISIBLE);
            }

            if ("1".equals(data.getIsRegister())) {
                tvManage.setVisibility(View.VISIBLE);
            } else {
                tvManage.setVisibility(View.GONE);
            }
            content = "确定要取消该活动？";
        }else{
            tvManage.setVisibility(View.GONE);
            tvAmend.setVisibility(View.GONE);
            tvCancel.setText("取消报名");
            content = "确定要取消报名？";
        }


        // 是否为我发布的
        if (isRelease) {
            if (data.getIsFee().equals("0")) {
                tvPrice.setText("免费");
            } else {
                tvPrice.setText("￥" + data.getFee());
            }
            tvAddress.setText(data.getAddress() + "");
        } else {
            tvPrice.setText("姓名：" + data.getSponsorName());
            tvAddress.setText("电话：" + data.getSponsorTel());
        }


        tvManage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                iCallBackActivity.manage(getAdapterPosition());
            }
        });
        tvAmend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                iCallBackActivity.edit(getAdapterPosition());
            }
        });
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new MaterialDialog.Builder(getContext())
                        .title("提示")
                        .content(content)
                        .positiveText("确定")
                        .negativeText("取消")
                        .onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                iCallBackActivity.cancel(getAdapterPosition());
                            }
                        })
                        .show();
            }
        });
    }

}
